﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using UraniaLib;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.ObjectModel;




namespace SkyPoint
{
    public partial class MainPage : PhoneApplicationPage
    {
        MotionBackend backend;
        DispatcherTimer dispatcherTimer;
        PlanetUtils planetUtils;
        StarLocator starLocator;
        ObservableCollection<SkyObject> nearbyObjects;

        bool active;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            backend = new MotionBackend();
            planetUtils = new PlanetUtils();
            starLocator = new StarLocator();
            nearbyObjects = new ObservableCollection<SkyObject>();

            active = false;
            starBox.ItemsSource = nearbyObjects;

            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
            dispatcherTimer.Tick += dispatcherTimer_Tick;
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            if (active)
            {
                startButton.Content = "Start";
                backend.StopSensors();
                dispatcherTimer.Stop();
                active = false;
            }
            else
            {
                startButton.Content = "Stop";
                backend.UpdatePosition();
                backend.StartSensors();
                dispatcherTimer.Start();
                active = true;
            }
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            double azimuth = backend.Azimuth;
            double latitude = backend.Latitude;
            double elevation = backend.Elevation;
            double longitude = backend.Longitude;
            double declination = 0;
            double rightAscension = 0;

            if (azimuth != double.NaN && latitude != double.NaN && elevation != double.NaN)
            {
                double ha = 0;
                DateTime current = DateTime.UtcNow;
                UraniaCoord.ConvHorToEqu(latitude, elevation, azimuth, ref ha, ref declination);
                rightAscension = UraniaCoord.ConvHAToRA(ha, current, longitude);
                raValue.Text = rightAscension.ToString("F2");
                declValue.Text = declination.ToString("F2");
            }
            List<SkyObject> nearbyList = starLocator.GetNearbyObjects(new Coord(rightAscension, declination));
            nearbyObjects.Clear();
            foreach(SkyObject star in nearbyList)
            {
                nearbyObjects.Add(star);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }





    }
}